package com.cjm.one.goods.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.model.coupon.entity.CouponInfo;
import com.cjm.one.model.coupon.vo.CouponInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 29289
* @description 针对表【coupon_info(优惠券信息)】的数据库操作Mapper
* @createDate 2024-09-05 15:55:46
* @Entity com.one.coupon.entity.CouponInfo
*/
@Mapper
public interface CouponInfoMapper extends BaseMapper<CouponInfo> {

    Page<CouponInfoVo> selectCouponInfoAppPage(Page<CouponInfoVo> page, @Param("rangeType") Integer rangeType, @Param("customerId") Long customerId);

    List<CouponInfoVo> findCouponsByRange();

    @Update("UPDATE coupon_info SET use_count = use_count + 1 WHERE id = #{couponId}")
    void incrUseCount(@Param("couponId") Long couponId);

    @Update("UPDATE coupon_info SET receive_count = receive_count + 1 WHERE id = #{couponId}")
    void incrReceiveCount(@Param("couponId") Long couponId);

    @Update("<script>" +
            "UPDATE coupon_info SET use_count = use_count + 1 " +
            "WHERE id IN " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>#{id}</foreach>" +
            "</script>")
    void batchIncrUseCount(@Param("ids") List<Long> couponIds);

    int updateCouponStock(@Param("couponId") Long couponId,@Param("version") Integer version,@Param("now") LocalDateTime now);
    @Update("<script>" +
            "UPDATE coupon_info SET receive_count = receive_count + 1 " +
            "WHERE id IN " +
            "<foreach item='id' collection='couponIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach> " +
            "AND publish_count > receive_count" + // 防止超发
            "</script>")
    void incrementBatchReceiveCount(List<Long> couponIds);
}




